{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "35f50354-75d9-4190-a108-bdad530fda3c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前时间: 2025-06-12 13:39:18.294115\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime, timedelta\n",
    "\n",
    "# 当前时间\n",
    "now = datetime.now()\n",
    "print(\"当前时间:\", now)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f25436ac-119a-4720-8bcc-3bda5f825f7b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7天后: 2025-06-19 13:39:18.294115\n"
     ]
    }
   ],
   "source": [
    "# 加7天\n",
    "future_date = now + timedelta(days=7)\n",
    "print(\"7天后:\", future_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "72eecaf6-340f-4780-8df3-b29cc955f66e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2小时前: 2025-06-12 11:39:18.294115\n"
     ]
    }
   ],
   "source": [
    "# 减去2小时\n",
    "past_time = now - timedelta(hours=2)\n",
    "print(\"2小时前:\", past_time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9847797b-edfb-4ee5-9c49-5f28a7a50673",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "时间差（天）: 1\n",
      "总秒数: 99000.0\n"
     ]
    }
   ],
   "source": [
    "start_time = datetime(2023, 10, 1, 12, 0, 0)\n",
    "end_time = datetime(2023, 10, 2, 15, 30, 0)\n",
    "\n",
    "# 时间差\n",
    "time_diff = end_time - start_time\n",
    "print(\"时间差（天）:\", time_diff.days)\n",
    "print(\"总秒数:\", time_diff.total_seconds())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "cf5b9773-b60b-43ab-8ce6-42e1fe57afb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 增强型日期处理\n",
    "from dateutil.relativedelta import relativedelta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "db9f7dca-49e2-434c-9ad7-e9d6016fa516",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前日期: 2025-06-12 13:43:01.826616\n",
      "增加3个月后的日期: 2025-09-12 13:43:01.826616\n"
     ]
    }
   ],
   "source": [
    "current_date = datetime.now()\n",
    "print(\"当前日期:\", current_date)\n",
    "# 增加3个月\n",
    "\n",
    "new_date = current_date + relativedelta(months=3)\n",
    "print(\"增加3个月后的日期:\", new_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a63a48e9-638b-4930-bcb9-3de0493fe680",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前日期: 2023-01-31 00:00:00\n"
     ]
    }
   ],
   "source": [
    "# use pandas\n",
    "import pandas as pd\n",
    "\n",
    "current_date = pd.to_datetime(\"2023-01-31\")\n",
    "print(\"当前日期:\", current_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "14257939-9f71-4f5c-ac78-52f3325afef4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前日期: 2023-01-31 00:00:00\n"
     ]
    }
   ],
   "source": [
    "current_date = pd.to_datetime(\"2023.01.31\")\n",
    "print(\"当前日期:\", current_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e2a2bd80-2bef-40ac-913e-00fdf5034424",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "增加3个月后的日期: 2023-04-30 00:00:00\n"
     ]
    }
   ],
   "source": [
    "new_date = current_date + pd.DateOffset(months=3)\n",
    "print(\"增加3个月后的日期:\", new_date)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "06dfe92d-4968-4006-a673-edf425719cc6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "time.struct_time(tm_year=2025, tm_mon=6, tm_mday=11, tm_hour=23, tm_min=40, tm_sec=0, tm_wday=2, tm_yday=162, tm_isdst=-1)\n",
      "星期 2\n",
      "1749656400\n"
     ]
    }
   ],
   "source": [
    "a = \"2025-06-11 23:40:00\"\n",
    "# 将其转换为时间数组\n",
    "import time\n",
    "\n",
    "timeArray = time.strptime(a, \"%Y-%m-%d %H:%M:%S\")\n",
    "# 显示详细信息\n",
    "print(timeArray)\n",
    "print(\"星期\", timeArray.tm_wday)\n",
    "# 转换为时间戳:\n",
    "timeStamp = int(time.mktime(timeArray))\n",
    "# timeStamp == 1381419600\n",
    "print(timeStamp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "705c8f50-12ff-4eab-84e7-ec9da53da980",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前时间戳为: 1749707572.1122456\n"
     ]
    }
   ],
   "source": [
    "ticks = time.time()\n",
    "print(\"当前时间戳为:\", ticks)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "865d4395-a76f-464f-892d-e24d40e5a785",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本地时间, 明细信息 : time.struct_time(tm_year=2025, tm_mon=6, tm_mday=12, tm_hour=13, tm_min=56, tm_sec=26, tm_wday=3, tm_yday=163, tm_isdst=0)\n",
      "本地时间, 英文 : Thu Jun 12 13:56:26 2025\n",
      "2025.06.12 13:56:26\n",
      "Thu Jun 12 13:56:26 2025\n",
      "1517974832.0\n",
      "\n"
     ]
    }
   ],
   "source": [
    "localtime = time.localtime(time.time())\n",
    "print(\"本地时间, 明细信息 :\", localtime)\n",
    "\n",
    "localtime = time.asctime(time.localtime(time.time()))\n",
    "print(\"本地时间, 英文 :\", localtime)\n",
    "\n",
    "# 格式化成2016-03-20 11:45:39形式\n",
    "# or yyyy.mm.hh\n",
    "print(time.strftime(\"%Y.%m.%d %H:%M:%S\", time.localtime()))\n",
    "\n",
    "# 格式化成Sat Mar 28 22:24:24 2016形式\n",
    "print(time.strftime(\"%a %b %d %H:%M:%S %Y\", time.localtime()))\n",
    "\n",
    "# 将格式字符串转换为时间戳\n",
    "a = \"Wed Feb 07 11:40:32 2018\"\n",
    "print(time.mktime(time.strptime(a, \"%a %b %d %H:%M:%S %Y\")))\n",
    "print(\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "af584430-4053-4f5a-9c43-9b1750324962",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16 minutes ago\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "from my import dateUtil\n",
    "\n",
    "last_time = time.time() - 1000\n",
    "print(dateUtil.from_now(last_time))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
